package com.zut.dao;

import com.zut.domain.Practise;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PractiseDao {

    @Results(id = "groupWithPractise",
            value = {
                    @Result(property = "id", column = "practise_id", id = true),
                    @Result(property = "place", column = "practise_place"),
                    @Result(property = "subject", column = "practise_subject"),
                    @Result(property = "time", column = "interval_time"),
            })
    @Select("select * from practise")
    List<Practise> getAll();

    @Results(id = "groupWithPractise2",
            value = {
                    @Result(property = "place", column = "practise_place"),
                    @Result(property = "subject", column = "practise_subject"),
            })
    @Select("select distinct practise_place, practise_subject from practise where practise_subject = '科目二'")
    List<Practise> getTwo();

    @ResultMap("groupWithPractise2")
    @Select("select distinct practise_place, practise_subject from practise where practise_subject = '科目三'")
    List<Practise> getThree();
    @Insert("insert into practise (practise_id,practise_place,practise_subject,interval_time) " +
            "values(#{id},#{place},#{subject},#{time})")
    int insert(Practise practise);

    @Delete("delete from practise where practise_id = #{id}")
    int deleteById(String id);

    @Update("<script>                                                                "
            + "update practise                                                       "
            + "<set>                                                                 "
            + "<if test='place != null'> practise_place=#{place},               </if>"
            + "<if test='subject != null'> practise_subject=#{subject},         </if>"
            + "<if test='time != null'> interval_time=#{time},                  </if>"
            + "</set>                                                                "
            + "where practise_id = #{id}                                             "
            + "</script>                                                             ")
    int update(Practise practise);
    @Select("select distinct practise_place from practise")
    List<String> getPlace();

    @Results(id = "groupWithPractise4",
            value = {
                    @Result(property = "id", column = "practise_id", id = true),
                    @Result(property = "place", column = "practise_place"),
                    @Result(property = "subject", column = "practise_subject"),
                    @Result(property = "time", column = "interval_time"),
            })
    @Select("select * from practise where practise_place = #{place}")
    List<Practise> getByPlace(String place);

}
